package com.qxzn.app.page.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qxzn.app.api.util.EventBusManager;
import com.qxzn.app.bean.EventBusBean;
import com.qxzn.app.page.R;
import com.qxzn.app.util.AppUtil;

import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

public class MyDialog extends Dialog {

    private TextView tvTitle;
    private TextView tvMessage;
    private EditText etInput;

    public TextView getTvTitle() {
        return tvTitle;
    }

    public void setTvTitle(TextView tvTitle) {
        this.tvTitle = tvTitle;
    }

    public TextView getTvMessage() {
        return tvMessage;
    }

    public void setTvMessage(TextView tvMessage) {
        this.tvMessage = tvMessage;
    }

    public MyDialog(@NonNull Context context) {
        super(context);
    }

    public MyDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected MyDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public EditText getEtInput() {
        return etInput;
    }

    public void setEtInput(EditText etInput) {
        this.etInput = etInput;
    }

    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        private View contentView;
        private View divider;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;
        private boolean cancelOnTouchOutside = true;
        private boolean cancelAble;
        private boolean showClose;
        private boolean showBottomView = true;
        private TextView btnPositive;
        private TextView btnNegative;
        private int width;
        private int height;
        private int windowType;//0：普通弹窗；1：系统弹窗；默认为0
        private Bitmap imgBitmap;
        private int imgResource;
        private OnDismissListener dismissListener;
        private MyDialog dialog;

        public Builder(Context context) {
            this.context = context;
        }

        public MyDialog create() {
            dialog = new MyDialog(context, R.style.custom_dialog);
            View layout = LayoutInflater.from(context).inflate(R.layout.dialog_common, null);
            dialog.tvTitle = ((TextView) layout.findViewById(R.id.title));
            dialog.tvTitle.setText(title);
            dialog.tvMessage = ((TextView) layout.findViewById(R.id.message));
            if (contentView != null) {
                dialog.tvMessage.setVisibility(View.GONE);
                LinearLayout ll_content = ((LinearLayout) layout.findViewById(R.id.ll_content));
                ll_content.addView(contentView);
            } else {
                if (!TextUtils.isEmpty(message)) {
                    dialog.tvMessage.setText(message);
                    dialog.tvMessage.setMovementMethod(ScrollingMovementMethod.getInstance());
                }
            }
            divider = layout.findViewById(R.id.divider);
            btnPositive = ((TextView) layout.findViewById(R.id.positiveButton));
            btnNegative = ((TextView) layout.findViewById(R.id.negativeButton));
            if (!TextUtils.isEmpty(positiveButtonText)) {
                btnPositive.setText(positiveButtonText);
            }
            if (!TextUtils.isEmpty(negativeButtonText)) {
                btnNegative.setText(negativeButtonText);
            }
            if (positiveButtonClickListener != null) {
                btnPositive.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                    }
                });
            } else {
                btnPositive.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            }
            if (negativeButtonClickListener != null) {
                btnNegative.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                    }
                });
            } else {
                btnNegative.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            }
            if (showClose) {
                ((ImageView) layout.findViewById(R.id.iv_close)).setVisibility(View.VISIBLE);
                ((ImageView) layout.findViewById(R.id.iv_close)).setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            }
            if (!showBottomView) {
                layout.findViewById(R.id.ll_bottom).setVisibility(View.GONE);
            }
//            if (width != 0) {
//                dialog.setContentView(layout, new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT));
//            } else {
//                dialog.setContentView(layout);
//            }
            dialog.setContentView(layout);
            dialog.setCancelable(cancelAble);
            dialog.setCanceledOnTouchOutside(cancelOnTouchOutside);
            dialog.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    if (dismissListener != null) {
                        dismissListener.onDismiss(dialog);
                    }
                    EventBusManager.post(new EventBusBean.Builder().id(8).build());
                }
            });
            Window window = null;
            if (width > 0 && height > 0) {
                window = dialog.getWindow();
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = width;
                params.height = height;
                window.setAttributes(params);
            } else if (width > 0 && height <= 0) {
                window = dialog.getWindow();
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = width;
                params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                window.setAttributes(params);
            } else if (width <= 0 && height > 0) {
                window = dialog.getWindow();
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                params.height = height;
                window.setAttributes(params);
            }
            if (windowType == 1) {
                if (window == null) {
                    window = dialog.getWindow();
                }
                window.setType(TYPE_SYSTEM_ALERT);
            }
//            dialog.setStatusBar();
            return dialog;
        }

        public MyDialog createInputDialog() {
            dialog = new MyDialog(context, R.style.custom_dialog);
            View layout = LayoutInflater.from(context).inflate(R.layout.dialog_input, null);
            dialog.tvTitle = ((TextView) layout.findViewById(R.id.title));
            dialog.tvTitle.setText(title);
            dialog.etInput = ((EditText) layout.findViewById(R.id.et_input));
            if (contentView != null) {
                dialog.etInput.setVisibility(View.GONE);
                LinearLayout ll_content = ((LinearLayout) layout.findViewById(R.id.ll_content));
                ll_content.addView(contentView);
            } else {
                if (!TextUtils.isEmpty(message)) {
                    dialog.etInput.setText(message);
                    dialog.etInput.setMovementMethod(ScrollingMovementMethod.getInstance());
                }
            }
            divider = layout.findViewById(R.id.divider);
            btnPositive = ((TextView) layout.findViewById(R.id.positiveButton));
            btnNegative = ((TextView) layout.findViewById(R.id.negativeButton));
            if (!TextUtils.isEmpty(positiveButtonText)) {
                btnPositive.setText(positiveButtonText);
            }
            if (!TextUtils.isEmpty(negativeButtonText)) {
                btnNegative.setText(negativeButtonText);
            }
            if (positiveButtonClickListener != null) {
                btnPositive.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                    }
                });
            } else {
                btnPositive.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            }
            if (negativeButtonClickListener != null) {
                btnNegative.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                    }
                });
            } else {
                btnNegative.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            }
            if (showClose) {
                ((ImageView) layout.findViewById(R.id.iv_close)).setVisibility(View.VISIBLE);
                ((ImageView) layout.findViewById(R.id.iv_close)).setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            }
            if (!showBottomView) {
                layout.findViewById(R.id.ll_bottom).setVisibility(View.GONE);
            }
//            if (width != 0) {
//                dialog.setContentView(layout, new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT));
//            } else {
//                dialog.setContentView(layout);
//            }
            dialog.setContentView(layout);
            dialog.setCancelable(cancelAble);
            dialog.setCanceledOnTouchOutside(cancelOnTouchOutside);
            dialog.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    if (dismissListener != null) {
                        dismissListener.onDismiss(dialog);
                    }
                    EventBusManager.post(new EventBusBean.Builder().id(8).build());
                }
            });
            Window window = null;
            if (width > 0 && height > 0) {
                window = dialog.getWindow();
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = width;
                params.height = height;
                window.setAttributes(params);
            } else if (width > 0 && height <= 0) {
                window = dialog.getWindow();
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = width;
                params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                window.setAttributes(params);
            } else if (width <= 0 && height > 0) {
                window = dialog.getWindow();
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                params.height = height;
                window.setAttributes(params);
            }
            if (windowType == 1) {
                if (window == null) {
                    window = dialog.getWindow();
                }
                window.setType(TYPE_SYSTEM_ALERT);
            }
//            dialog.setStatusBar();
            return dialog;
        }

        public MyDialog createImgDialog() {
            dialog = new MyDialog(context, R.style.custom_dialog);
            ImageView iv_dialog = new ImageView(context);
            iv_dialog.setScaleType(ImageView.ScaleType.FIT_XY);
            iv_dialog.setAdjustViewBounds(true);
            if (imgBitmap != null) {
                iv_dialog.setImageBitmap(imgBitmap);
            } else {
                iv_dialog.setImageResource(imgResource);
            }
            dialog.setContentView(iv_dialog);
            dialog.setCancelable(cancelAble);
            dialog.setCanceledOnTouchOutside(cancelOnTouchOutside);
            dialog.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    if (dismissListener != null) {
                        dismissListener.onDismiss(dialog);
                    }
                    EventBusManager.post(new EventBusBean.Builder().id(8).build());
                }
            });
            iv_dialog.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (positiveButtonClickListener != null) {
                        positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                    }
                    dialog.dismiss();
                }
            });
            Window window = null;
            if (width > 0 && height > 0) {
                AppUtil.showLog(1);
                window = dialog.getWindow();
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = width;
                params.height = height;
                window.setAttributes(params);
            } else if (width > 0 && height <= 0) {
                AppUtil.showLog(2);
                window = dialog.getWindow();
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = width;
                params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                window.setAttributes(params);
            } else if (width <= 0 && height > 0) {
                AppUtil.showLog(3);
                window = dialog.getWindow();
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                params.height = height;
                window.setAttributes(params);
            } else {
                AppUtil.showLog(4);
            }
            if (windowType == 1) {
                if (window == null) {
                    window = dialog.getWindow();
                }
                window.setType(TYPE_SYSTEM_ALERT);
            }
//            dialog.setStatusBar();
            return dialog;
        }

        public MyDialog createCustomDialog() {
            dialog = new MyDialog(context, R.style.custom_dialog);
            dialog.setContentView(contentView);
            dialog.setCancelable(cancelAble);
            dialog.setCanceledOnTouchOutside(cancelOnTouchOutside);
            dialog.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    if (dismissListener != null) {
                        dismissListener.onDismiss(dialog);
                    }
                    EventBusManager.post(new EventBusBean.Builder().id(8).build());// TODO: 2019/5/8
                }
            });
            Window window = null;
            if (width > 0 && height > 0) {
                window = dialog.getWindow();
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = width;
                params.height = height;
                window.setAttributes(params);
            } else if (width > 0 && height <= 0) {
                window = dialog.getWindow();
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = width;
                params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                window.setAttributes(params);
            } else if (width <= 0 && height > 0) {
                window = dialog.getWindow();
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                params.height = height;
                window.setAttributes(params);
            }
            if (windowType == 1) {
                if (window == null) {
                    window = dialog.getWindow();
                }
                window.setType(TYPE_SYSTEM_ALERT);
            }
//            dialog.setStatusBar();
            return dialog;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        public Builder setPositiveButtonText(String positiveButtonText) {
            this.positiveButtonText = positiveButtonText;
            return this;
        }

        public Builder setNegativeButtonText(String negativeButtonText) {
            this.negativeButtonText = negativeButtonText;
            return this;
        }

        public Builder setPositiveButton(OnClickListener listener) {
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(OnClickListener listener) {
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setCanceledOnTouchOutside(boolean cancelOnTouchOutside) {
            this.cancelOnTouchOutside = cancelOnTouchOutside;
            if (dialog != null) {
                dialog.setCancelable(cancelOnTouchOutside);
            }
            return this;
        }

        public Builder setCancelAble(boolean cancelAble) {
            this.cancelAble = cancelAble;
            if (dialog != null) {
                dialog.setCancelable(cancelAble);
            }
            return this;
        }

        public Builder setShowClose(boolean showClose) {
            this.showClose = showClose;
            return this;
        }

        public int getWidth() {
            return width;
        }

        public Builder setWidth(int width) {
            this.width = width;
            return this;
        }

        public OnDismissListener getDismissListener() {
            return dismissListener;
        }

        public Builder setDismissListener(OnDismissListener dismissListener) {
            this.dismissListener = dismissListener;
            return this;
        }

        public int getHeight() {
            return height;
        }

        public Builder setHeight(int height) {
            this.height = height;
            return this;
        }

        public Builder setImgBitmap(Bitmap imgBitmap) {
            this.imgBitmap = imgBitmap;
            return this;
        }

        public Builder setImgResource(int imgResource) {
            this.imgResource = imgResource;
            return this;
        }

        public Builder setWindowType(int windowType) {
            this.windowType = windowType;
            return this;
        }

        public boolean isShowBottomView() {
            return showBottomView;
        }

        public Builder setShowBottomView(boolean showBottomView) {
            this.showBottomView = showBottomView;
            return this;
        }

        public Builder setPositiveBtnVisible(int visible) {
            btnPositive.setVisibility(visible);
            return this;
        }

        public Builder setNegativeBtnVisible(int visible) {
            btnNegative.setVisibility(visible);
            return this;
        }

        public Builder setDividerVisible(int visible) {
            divider.setVisibility(visible);
            return this;
        }

    }

    protected void setStatusBar() {
//        if (Build.VERSION.SDK_INT < 16) {
//            this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        } else {
        int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN //hide statusBar
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; //hide navigationBar
        getWindow().getDecorView().setSystemUiVisibility(uiFlags);
//        }
    }
}
